import { defineStore } from "pinia";
import {
  getSystemStats,
  getUserGrowthData,
  getSalesData,
  getOrderStatusData,
  getNotifications,
  markNotificationRead,
  getSystemConfig,
} from "@/api/system.js";

export const useSystem = defineStore("useSystem", {
  state: () => {
    return {
      systemStats: {
        totalUsers: 0,
        totalOrders: 0,
        totalRevenue: 0,
        totalProducts: 0,
      },
      chartData: {
        userGrowth: [],
        salesData: [],
        orderStatus: [],
      },
      notifications: [],
      unreadCount: 0,
      systemConfig: {},
    };
  },
  getters: {
    // 获取未读通知数量
    getUnreadCount: (state) => state.unreadCount,
    // 获取系统配置
    getSystemConfig: (state) => state.systemConfig,
  },
  actions: {
    /**
     * @description: 获取系统统计数据
     */
    async fetchSystemStats() {
      try {
        const res = await getSystemStats();
        this.systemStats = res;
        return res;
      } catch (error) {
        console.error("获取系统统计数据失败:", error);
        throw error;
      }
    },

    /**
     * @description: 获取用户增长数据
     */
    async fetchUserGrowthData() {
      try {
        const res = await getUserGrowthData();
        this.chartData.userGrowth = res;
        return res;
      } catch (error) {
        console.error("获取用户增长数据失败:", error);
        throw error;
      }
    },

    /**
     * @description: 获取销售数据
     */
    async fetchSalesData() {
      try {
        const res = await getSalesData();
        this.chartData.salesData = res;
        return res;
      } catch (error) {
        console.error("获取销售数据失败:", error);
        throw error;
      }
    },

    /**
     * @description: 获取订单状态数据
     */
    async fetchOrderStatusData() {
      try {
        const res = await getOrderStatusData();
        this.chartData.orderStatus = res;
        return res;
      } catch (error) {
        console.error("获取订单状态数据失败:", error);
        throw error;
      }
    },

    /**
     * @description: 获取通知列表
     */
    async fetchNotifications() {
      try {
        const res = await getNotifications();
        this.notifications = res.list;
        this.unreadCount = res.unreadCount;
        return res;
      } catch (error) {
        console.error("获取通知列表失败:", error);
        throw error;
      }
    },

    /**
     * @description: 标记通知为已读
     */
    async markNotificationAsRead(id) {
      try {
        await markNotificationRead(id);
        // 更新本地状态
        const notification = this.notifications.find((n) => n.id === id);
        if (notification) {
          notification.isRead = true;
          this.unreadCount = Math.max(0, this.unreadCount - 1);
        }
      } catch (error) {
        console.error("标记通知已读失败:", error);
        throw error;
      }
    },

    /**
     * @description: 获取系统配置
     */
    async fetchSystemConfig() {
      try {
        const res = await getSystemConfig();
        this.systemConfig = res;
        return res;
      } catch (error) {
        console.error("获取系统配置失败:", error);
        throw error;
      }
    },

    /**
     * @description: 初始化系统数据
     */
    async initSystemData() {
      try {
        await Promise.all([
          this.fetchSystemStats(),
          this.fetchUserGrowthData(),
          this.fetchSalesData(),
          this.fetchOrderStatusData(),
          this.fetchNotifications(),
          this.fetchSystemConfig(),
        ]);
      } catch (error) {
        console.error("初始化系统数据失败:", error);
        throw error;
      }
    },
  },
});
